package com.cloudptt.api.product.func.call.group.fsm.call;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.product.func.call.CallConst;
import com.cloudptt.api.product.func.call.group.fsm.IRollback;
import com.cloudptt.api.product.func.call.group.fsm.RollbackTimer;
import com.cloudptt.api.product.log.Log;

import android.content.Context;


public class ConnectingState implements ICallState,IRollback
{
	private GroupCallAgent agent = null;
	private RollbackTimer rollbackTimer = null;

	public ConnectingState(Context ctx,GroupCallAgent agent)
	{
		this.agent = agent;
		this.rollbackTimer = new RollbackTimer(ctx,6,this);
	}
	public int doUserAction(int btnAction)
	{
		if(CallConst.BtnAction_Hangup == btnAction)
		{
			int result = this.agent.doHangupAction();
			if(CauseCont.OperateCheck_Suc == result)
			{
//				gotoDisconnectingState();//呼叫未成功时，松开按钮，则发cancel结束呼叫
				this.agent.goToState(CallConst.CallState_DisconnectingState);
			}
			return result;
		}
		else if(CallConst.BtnAction_Accept == btnAction)
		{
			int result = this.agent.doAcceptAction();
//			if(CauseCont.OperateCheck_Suc == result)
//			{
////				gotoDisconnectingState();
//				this.agent.goToState(GroupCallConst.CallState_DisconnectingState);
//			}
			return result;
		}
		return CauseCont.OperateErr_Invalid;
	}
	public void receiveCallAction(int talkAction)
	{
		if(CallConst.Session_Confirmed_Active == talkAction)
		{
//			gotoActiviteState();
			this.agent.goToState(CallConst.CallState_Activite);
		}
		else if(CallConst.Session_Disconnected == talkAction)
		{
//			gotoInitialState();
			this.agent.goToState(CallConst.CallState_InitState);
		}
	}
	//	public void gotoTalkState(int talkAction)
	//	{
	//		ICallState currentState = new ActiviteState(this.agent);
	//		this.agent.setCurrentState(currentState);
	//		this.agent.receiveCallAction(talkAction);
	//	}
//	public void gotoActiviteState()
//	{
//		ICallState currentState = new ActiviteState(this.agent);
//		this.agent.setCurrentState(currentState);
//	}
//	public void gotoInitialState()
//	{
//		ICallState currentState = new InitialState(this.agent);
//		this.agent.setCurrentState(currentState);
//	}
//	public void gotoDisconnectingState()
//	{
//		ICallState currentState = new DisconnectingState(this.agent);
//		this.agent.setCurrentState(currentState);
//	}

	public void rollbackState()
	{
		Log.d(GroupCallAgent.Log_Key, "CallingState rollbackState");
		if(agent != null)
		{
			this.agent.doHangupAction();
			ICallState currentState = new InitialState(this.agent);
			this.agent.setCurrentState(currentState);
		}
	}


	public int getState()
	{
		return CallConst.CallState_ConnectingState;
	}
	public void destory()
	{
		if(rollbackTimer != null)
		{
			rollbackTimer.cancelTimer();
			rollbackTimer = null;
		}
		agent = null;
	}
	@Override
	public void rollback() {
		// TODO Auto-generated method stub
		Log.d(GroupCallAgent.Log_Key, "CallingState rollbackState");
		if(agent != null)
		{
			int result = this.agent.doHangupAction();
			if(CauseCont.OperateCheck_Suc == result)
			{
//				gotoDisconnectingState();
				this.agent.goToState(CallConst.CallState_DisconnectingState);
			}
		}
		
	}
}
